package com.yanqu.road.dao.impl.activity.guguoxunbao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.guguoxunbao.GuGuoXunBaoUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GuGuoXunBaoUserDataDaoImpl extends TempDataObjectDao<GuGuoXunBaoUserData> {


    public static String getGuGuoXunBaoUserDataSql = "select * from t_u_activity_guguoxunbao where activityId=? and userId=?";
    public GuGuoXunBaoUserData getGuGuoXunBaoUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getGuGuoXunBaoUserDataSql);
            int i = 1;
            call.setInt(i++,activityId);
                call.setLong(i++,userId);
                rs = call.executeQuery();
                while (rs.next()) {
                    GuGuoXunBaoUserData data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertGuGuoXunBaoUserDataSql = "insert into t_u_activity_guguoxunbao(activityId,userId,allClue,curProgress,treasureMap,curPlaceId) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(GuGuoXunBaoUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAllClue()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getCurProgress())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getTreasureMap())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurPlaceId()));
        if (getDb().executeNoneQuery(insertGuGuoXunBaoUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateGuGuoXunBaoUserDataSql = "update t_u_activity_guguoxunbao set allClue=?,curProgress=?,treasureMap=?,curPlaceId=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(GuGuoXunBaoUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getAllClue()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getCurProgress())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getTreasureMap())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurPlaceId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateGuGuoXunBaoUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected GuGuoXunBaoUserData parseData(ResultSet rs) throws SQLException {
        GuGuoXunBaoUserData data = new GuGuoXunBaoUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setAllClue(rs.getLong("allClue"));
        data.setCurPlaceId(rs.getInt("curPlaceId"));
        String str  = rs.getString("curProgress");
        if(!StringUtils.isNullOrEmpty(str)){
            Map<Integer, Integer> result = JSON.parseObject(str, new TypeReference<ConcurrentHashMap<Integer, Integer>>() {});
            if(result != null){
                data.setCurProgress(result);
            }
        }
        str  = rs.getString("treasureMap");

        if(!StringUtils.isNullOrEmpty(str)){
            Map<Integer, Long> result = JSON.parseObject(str, new TypeReference<ConcurrentHashMap<Integer, Long>>() {});
            if(result != null){
                data.setTreasureMap(result);
            }
        }

        return data;
    }
}
